package com.wxh.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: Test5
 * @Description:
 * @Author wxh
 * @Date: 2022/3/29 11:31
 * @Version V1.0.0
 * @Since 1.8
 */
public class Test5 {
    public static void main(String[] args) {
//        String str = "张三的小毛驴".replaceAll("张三","断句"+"张三");
//        System.out.println(str);
//
//        boolean flag1 = true;
//        boolean flag2 = true;
//        if(!flag1 && flag2){
//            System.out.println("``````````````````");
//        }else{
//            System.out.println("2222222222222");
//        }
//
//        System.out.println("---------------华丽分割线  -----------");
//
//        getPrevMonthDate();
//
//        getLastDayOfMonth("2022-02");
//        getFirstDayOfMonth("2022-02");
//        System.out.println(getPrevTwoDay());
//        System.out.println(getCurrentSystemTime());
        boolean a = true;
        boolean b = true;
        if(!a && !b){
            System.out.println("nono");
        }else{
            System.out.println("okok");
        }
    }

    public static String getPrevTwoDay(){
        try {

            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            Date dBefore = calendar.getTime();
            String dateStr = sdf.format(dBefore);

            Date date2 = sdf.parse(dateStr);

//            Calendar calendar2 = Calendar.getInstance();
            calendar.setTime(date2);
            calendar.add(Calendar.DAY_OF_MONTH, -2);
            Date dBefore2 = calendar.getTime();
            String dateStr2 = sdf.format(dBefore2);

            return dateStr2;
        } catch (ParseException e) {
            throw new IllegalArgumentException("parameter date is not valid");
        }
    }

    public static String getCurrentSystemTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date dBefore = calendar.getTime();
        String dateStr = sdf.format(dBefore);
        return dateStr;
    }


    public static void getPrevMonthDate(){
        try {
            String startTime = "2022-03-29 00:00:00";
            String endTime = null;
            int type = 43200;
            int dateType = 0;
            int number = 0;
            Calendar calendar = Calendar.getInstance();
            Date startTimeByDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
            calendar.setTime(startTimeByDate);

            if(43200 == type){
                calendar.add(Calendar.MONTH, -1);
                dateType = Calendar.MONTH;
                number = 1;
                endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
            }
            System.out.println(endTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定年月的最后一天(获取指定月份的下个月第一天，其减去一天)
     * @param yearMonth 年月，比如2022-02
     * @return String
     */
    public static String getLastDayOfMonth(String yearMonth){
        int year = Integer.parseInt(yearMonth.split("-")[0]);
        int month = Integer.parseInt(yearMonth.split("-")[1]);

        Calendar calendar = Calendar.getInstance();
        // 指定日历中年份
        calendar.set(Calendar.YEAR, year);
        // 设置当前月份的下一个月，月份下标是从零开始
        calendar.set(Calendar.MONTH, month);
        // 获取月份中最小值，就是第一天
        int firstDay = calendar.getMinimum(Calendar.DATE);
        // 设置月份中最大的天数，下个月第一天减去1，就是上个月的最后一天
        calendar.set(Calendar.DAY_OF_MONTH, firstDay - 1);

        Date dateTime = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(dateTime);
        // 2022-02-28
        System.out.println(dateStr);
        return dateStr;
    }

    /**
     * 获取指定年月的第一天
     * @param yearMonth 年月，比如2022-02
     * @return String
     */
    public static String getFirstDayOfMonth(String yearMonth){
        int year = Integer.parseInt(yearMonth.split("-")[0]);
        int month = Integer.parseInt(yearMonth.split("-")[1]);

        Calendar calendar = Calendar.getInstance();
        // 指定日历中年份
        calendar.set(Calendar.YEAR, year);
        // 设置当前月份月份下标是从零开始
        calendar.set(Calendar.MONTH, month - 1);
        // 获取月份中最小值，就是第一天
        int firstDay = calendar.getMinimum(Calendar.DATE);
        // 设置月份中最小的天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);

        Date dateTime = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(dateTime);
        // 2022-02-01
        System.out.println(dateStr);
        return dateStr;
    }
}
